package com.foton.logistic.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.foton.logistic.R;
import com.foton.logistic.base.BaseActivity;
import com.foton.logistic.util.Idcard;
import com.foton.logistic.util.OptionUtil;
import com.foton.logistic.util.SharedUtil;
import com.foton.logistic.util.tool.StringUtil;
import com.foton.logistic.view.CustomPopWindow;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by wuhaocong on 2017/7/5.
 */

public class SupplyActivity extends BaseActivity {
    @InjectView(R.id.person_info_shop_text)
    TextView personInfoShopText;
    @InjectView(R.id.person_info_shop)
    RelativeLayout personInfoShop;
    @InjectView(R.id.person_info_name)
    EditText personInfoName;
    @InjectView(R.id.person_info_gender_text)
    TextView personInfoGenderText;
    @InjectView(R.id.person_info_gender)
    RelativeLayout personInfoGender;
    @InjectView(R.id.person_info_sf_text)
    EditText personInfoSf;
    @InjectView(R.id.person_info_js)
    EditText personInfoJs;
    @InjectView(R.id.person_info_supply)
    LinearLayout personInfoSupply;
    private CustomPopWindow mCustomPopWindow;
    private String InfoName;
    private String InfoSf;
    private String InfoJs;
    private String gender;
    private String InfoShop;
    private String InfoGender;
    private String carrierId;
    private String[] abnormalArray1;
    ArrayList<String> list=new ArrayList<>();
    ArrayList<String>list1=new ArrayList<>();
    ArrayList<String>list2=new ArrayList<>();
    private String s;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ft_ui_supply);
        ButterKnife.inject(this);
    }

    @Override
    public void init() {
        setBackLayoutVisibility(View.VISIBLE);
        setTitleText("补充信息");
        setTitleTextVisibility(View.VISIBLE);
       // registerBroad();

    }
   /* ReceiveBroadcast receiveBroadcast;
    private void registerBroad() {
        if (receiveBroadcast == null) {
            receiveBroadcast = new ReceiveBroadcast();
            IntentFilter filter = new IntentFilter();
            filter.addAction(BaseConstant.ACTION_SELECT_FILTER);
            this.registerReceiver(receiveBroadcast, filter);
        }
    }
    public class ReceiveBroadcast extends BroadcastReceiver {



        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                String action = intent.getAction();
                if (action.equals(BaseConstant.ACTION_SELECT_FILTER)) {
                    SelectEntity selectEntity = (SelectEntity) intent.getSerializableExtra(BaseConstant.INTENT_SELECT);
                    carrierId =selectEntity.selectcarrierId;
                    //personInfoShopText.setText(selectEntity.selectname);
                    SharedUtil.saveCarrierId1(SupplyActivity.this,carrierId);
                   *//* if (recommendFilterEntity.type == 0) {
                        startTime = recommendFilterEntity.startTime;
                        endTime = recommendFilterEntity.endTime;
                        brandName =recommendFilterEntity.brand;
                        refresh(true);
                    }*//*
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }*/
    @OnClick({R.id.person_info_shop, R.id.ft_ui_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.person_info_shop:
                AlertDialog.Builder builder = new AlertDialog.Builder(SupplyActivity.this);
                //builder.setIcon(R.drawable.ic_launcher);
                builder.setTitle("准驾车型");

                    abnormalArray1 = getResources().getStringArray(R.array
                            .abnormal_string_list5);


                //    设置一个单项选择下拉框
              /*  *
                 * 第一个参数指定我们要显示的一组下拉多选框的数据集合
                 * 第二个参数代表哪几个选项被选择，如果是null，则表示一个都不选择，如果希望指定哪一个多选选项框被选择，
                 * 需要传递一个boolean[]数组进去，其长度要和第一个参数的长度相同，例如 {true, false, false, true};
                 * 第三个参数给每一个多选项绑定一个监听器
*/
                builder.setMultiChoiceItems(abnormalArray1, null, new DialogInterface.OnMultiChoiceClickListener() {
                    //StringBuffer sb = new StringBuffer(100);

                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if (isChecked) {
                            list1.clear();
                            list.add(abnormalArray1[which]);
                            Log.e("+++++++++++", "onClick: "+list);
                            //sb.append(hobbies[which] + ", ");
                            Log.e("___________", "onClick: "+list1);
                        }else {
                            list1.add(abnormalArray1[which]);
                            list.removeAll(list1);
                            //list.remove(which);
                            Log.e("+++++++++++", "onClick: "+list);
                            Log.e("___________", "onClick: "+list1);
                        }
                        list2.clear();
                        for (int i = 0; i < list.size(); i++) {
                            list2.add(list.get(i));
                        }

                        //s =new String(sb);
                        // Toast.makeText(MainActivity.this, "爱好为：" + sb.toString(), Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    StringBuffer sb = new StringBuffer(100);
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int size=list.size();
                        String[] array = (String[])list.toArray(new String[size]);

                        for (int i = 0; i <array.length ; i++) {
                            sb.append(array[i] + " ");
                        }
                        s =new String(sb);
                        personInfoShopText.setText(s);
                        SharedUtil.saveCarType(SupplyActivity.this,s);
                        list.clear();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        list.clear();
                    }
                });
                builder.show();

               /* Intent intent = new Intent(this, SelectActivity.class);
                startActivityForResult(intent, 0);*/
                break;
            case R.id.ft_ui_next:
                InfoName =personInfoName.getText().toString().trim();
                InfoSf =personInfoSf.getText().toString().trim();
                InfoJs =personInfoJs.getText().toString().trim();
                InfoShop = personInfoShopText.getText().toString().trim();
                InfoGender = personInfoGenderText.getText().toString().trim();
                Idcard idcard=new Idcard();
                if (StringUtil.isEmpty(InfoShop)) {
                    OptionUtil.addToast(this, "准驾车型不能为空");
                    return;
                } else if (StringUtil.isEmpty(InfoName)) {
                    OptionUtil.addToast(this, "姓名不能为空");
                    return;
                }else if (StringUtil.isEmpty(InfoSf)) {
                    OptionUtil.addToast(this, "身份证不能为空");
                    return;
                } else if (StringUtil.isEmpty(InfoGender)) {
                    OptionUtil.addToast(this, "性别不能为空");
                    return;
                } else if (StringUtil.isEmpty(InfoJs)) {
                    OptionUtil.addToast(this, "驾照不能为空");
                    return;
                } else if (!idcard.verify(InfoSf)) {
                    OptionUtil.addToast(this, "请输入正确的身份证格式");
                    return;
                }
                else {
                    SharedUtil.saveName(this,InfoName);
                    SharedUtil.saveSf(this,InfoSf);
                    SharedUtil.saveJsCard(this,InfoJs);
                    Intent intent1 = new Intent(this, PictureActivity.class);
                    startActivity(intent1);
                }
                break;
        }
    }

    @OnClick(R.id.person_info_gender)
    public void onViewClicked() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_menu1, null);
        //处理popWindow 显示内容
        handleLogic(contentView);
        //创建并显示popWindow
        mCustomPopWindow = new CustomPopWindow.PopupWindowBuilder(this)
                .setView(contentView)
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .setBgDarkAlpha(0.7f) // 控制亮度
                .create()
                .showAtLocation(personInfoSupply, Gravity.CENTER, 0, 0);
    }

    private void handleLogic(View contentView) {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCustomPopWindow != null) {
                    mCustomPopWindow.dissmiss();
                }

                switch (v.getId()) {
                    case R.id.pop_shi:
                        personInfoGenderText.setText("男");
                        gender ="0";
                        SharedUtil.saveGender(SupplyActivity.this,"0");
                        break;
                    case R.id.pop_fou:
                        personInfoGenderText.setText("女");
                        gender ="1";
                        SharedUtil.saveGender(SupplyActivity.this,"1");
                        break;

                }

            }
        };
        contentView.findViewById(R.id.pop_shi).setOnClickListener(listener);
        contentView.findViewById(R.id.pop_fou).setOnClickListener(listener);
    }
    @Override
    public void onDestroy() {
        try {
            //this.unregisterReceiver(receiveBroadcast);
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }
}
